//
// Created by 崔世强 on 2018/3/30.
// Copyright (c) 2018 Facebook. All rights reserved.
//

import Foundation
import SnapKit

class GroupMemberController: ChatBaseController {
    
    var groupId: Int!
    
    var groupMembers:[ChatGroupMember]!
    
    let service = GroupService()
    
    
    
    lazy var collectionView: UICollectionView = {
        let collectionLayout = UICollectionViewFlowLayout()
        
        collectionLayout.itemSize = CGSize(width: (self.view.width - 30) / 5, height: 80)
        collectionLayout.minimumInteritemSpacing = 0
        collectionLayout.minimumLineSpacing = 0
        //设置item内边距
        collectionLayout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        
        let collection = UICollectionView(frame: CGRect.zero, collectionViewLayout: collectionLayout)
        collection.register(GroupMemberInfoCell.self, forCellWithReuseIdentifier: GroupMemberInfoCell.ID)
//        collection.backgroundColor = .white
        collection.dataSource = self
        collection.delegate = self
        return collection
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "群成员"
        
        do {
            if let g = try GroupDao.getGroup(groupId) {
                groupMembers = try service.getChatGroupMembers(groupId, limit: Int.max)
                
                self.view.addSubview(collectionView)
                collectionView.snp.makeConstraints { make in
                    make.edges.equalToSuperview()
                }
                
            }
        } catch {
            print(error)
        }
    }
}

extension GroupMemberController: UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return groupMembers.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let model = groupMembers[indexPath.item]
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GroupMemberInfoCell.ID, for: indexPath) as? GroupMemberInfoCell
        
        cell?.model = model
        
        return cell!
    }
}
